package com.magicode.cms.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IEmailNewsLetterDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.EmailContact;
import com.magicode.cms.entiy.base.EmailNewsLetter;
import com.magicode.cms.entiy.base.EmailNewsLetterRela;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsEnterpriseRela;
import com.magicode.cms.entiy.base.RepresentWorks;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.service.IEmailNewsLetterService;
@Service("serviceEmailNewsLetter")
public class EmailNewsLetterServiceImpl extends BaseServiceImpl<EmailNewsLetter,IEmailNewsLetterDao> implements IEmailNewsLetterService,UtilPath{

	@Autowired
	@Qualifier("daoEmailNewsLetter")
	private IEmailNewsLetterDao emailNewsLetterDao;


	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "template");
		map.put(json_title, "模板");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "nlNumber");
		map.put(json_title, "期次");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "status");
		map.put(json_title, "状态");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "sendTime");
		map.put(json_title, "发送时间");
		map.put("width", 120);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +EmailNewsLetter.class.getName()+" "+entity_alias_name+" ";
		hql +=" left join t.template as p ";
		hql += " left join t.rightNews as n ";
		hql += " where 1 = 1 ";
	if(null!=querywhere && !querywhere.isEmpty()){
		if(querywhere.containsKey(field_id)){
			hql = hql+" and "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
		}
		if(querywhere.containsKey("title")){
			hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get("title")+"%'";
		}
		if(querywhere.containsKey("status")){
			hql = hql+" and "+entity_alias_name+".status = '"+querywhere.get("status")+"'";
		}
	}
	return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("template", " p.title as template ");
			gridsel.put("nlNumber", ""+entity_alias_name+".nlNumber as nlNumber ");
			gridsel.put("status", "case when "+entity_alias_name+".status = 0 then '草稿' "
					+ " when "+entity_alias_name+".status = 1 then '生成静态页面' "
					+ "  end as status ");
			gridsel.put("sendTime", ""+entity_alias_name+".sendTime as sendTime ");
			return this.queryMapDataList(hql, gridsel,start ,size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public List<EmailNewsLetterRela> getEmailNewsLetterRela(String id) {
		List<EmailNewsLetterRela> videoList = new ArrayList();
//		EmailNewsLetter enterprise = null;
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("id", id);
//		String [] fetchJoins = new String[4];
//		fetchJoins[0] = "rightNews";
//		fetchJoins[1] = "template";;
//		fetchJoins[2] = "emailNewsLetterRelaList";
//		try {
//			enterprise =super.getObject(id,fetchJoins);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		videoList = enterprise.getEmailNewsLetterRelaList();
		String hql ="from " +EmailNewsLetterRela.class.getName()+" "+entity_alias_name+" ";
		hql +=" left outer join fetch t.news as n ";
		hql +=" left outer join fetch t.emailNewsLetter  as l";
		hql +=" where l.id=:id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			videoList=this.emailNewsLetterDao.getCommonDaoService().queryList(hql, map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return videoList;
	}
	
	@Override
	public List<EmailContact> getEmailContact(String id) {
		List<EmailContact> videoList = new ArrayList();
		EmailNewsLetter enterprise = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		String [] fetchJoins = new String[3];
		fetchJoins[0] = "rightNews";
		fetchJoins[1] = "template";
		fetchJoins[2] = "emailContacts";
		try {
			enterprise =super.getObject(id,fetchJoins);
		} catch (Exception e) {
			e.printStackTrace();
		}
		videoList = enterprise.getEmailContacts();
		return videoList;
	}
	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		Date createDate = null;
		EmailNewsLetter emailNewsLetter = null;
		//用来格式化时间格式，方便在预览的时候请求静态资源
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		String dateParam = "";
		try {
			//获取当前资讯信息
			emailNewsLetter = super.getObject(id);
			if(null != emailNewsLetter){
				createDate = emailNewsLetter.getCreateTime();
				dateParam = df.format(createDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Integer status = null;
		try {
			emailNewsLetter = super.getObject(id);
			status = emailNewsLetter.getStatus();
		} catch (Exception e) {
			e.printStackTrace();
		}
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		String staticHtmlUrl = emailNewsLetter.getStaticPagePath();
		if(null != staticHtmlUrl && !"".equals(staticHtmlUrl)){
			opt.setUrl(getCtx().getContextPath()+ staticHtmlUrl); 
		}else{
			opt.setUrl(getCtx().getContextPath()+"email/newsletter/" + id); 
		}
//		opt.setUrl(getCtx().getContextPath()+"/p/news/article/sec/"+ dateParam +"/" + id); 
		opt.setType("preview");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("直接发送");
		opt.setUrl(getCtx().getContextPath()+"/mail/send/" + id); 
		opt.setType("send");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("编辑并发送");
		opt.setUrl(getCtx().getContextPath()+"/mail?emailNewsLetterId=" + id); 
		opt.setType("edit");
		opts.add(opt);
		return opts;
	}
	@Override
	public IEmailNewsLetterDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return emailNewsLetterDao;
	}

	public IEmailNewsLetterDao getEmailNewsLetterDao() {
		return emailNewsLetterDao;
	}

	public void setEmailNewsLetterDao(IEmailNewsLetterDao emailNewsLetterDao) {
		this.emailNewsLetterDao = emailNewsLetterDao;
	}

}
